var mysetInterval = (func, timeout) => {
  let timer = null;
  const interval = () => {
    timer = setTimeout(() => {
      func();
      interval();
    }, timeout);
  }

  interval();
  return timer;
}

const timer = mysetInterval(() => {
  console.log(1);
}, 300);

setTimeout(() => {
  clearTimeout(timer);
}, 3000);